// pages/fineBalance/fineBalance.js
var util = require('../../utils/request.js');
var globalData = getApp().globalData;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    order:[],
    pageNumber: 1,
    pageSize: 20,
    hasMoreData: true,
    detail: ['余额明细', '微信支付明细'],
    detailCheck: 0,
    currentTab: 0,
    tabs: ['充值明细', '余额明细']
  },
  // tab切换
  //改变选中的账单类型
  chooseDetail: function (e) {
    console.log(e);
    this.setData({
      detailCheck: e.currentTarget.id,
      order: [],
      pageNumber: 1
    })
    if(e.currentTarget.id == 0) {
      this.bill('balance');
    } else {
      this.bill('wechat');
    }
  },
  // 获取账单
  bill: function (method) {
    var that = this;
    var order = that.data.order;
    var pageNumber = that.data.pageNumber;
    var pageSize = that.data.pageSize;
    var data, url;
    if(method == 'balance') {
      data = {
        "memberId": globalData.userInfo.memberId, 
        "method": 'balance', 
        "pageNumber": pageNumber, 
        "pageSize": pageSize
      }
      url = "/member/deposit";
    } else {
      data = {
        "memberId": globalData.userInfo.memberId,
        "pageNumber": pageNumber,
        "pageSize": pageSize
      }
      url = "/member/payment";
    }
    util.req(url, data, function (res) {
      console.log(res);
      if (res.resultCode == 200) {
        res.resultContent.map(function(item) {
          item.time = util.formatTime(new Date(item.time));
        })
        console.log(res.resultContent);
        if (res.resultContent.length < pageSize) {
          that.setData({
            hasMoreData: false,
            order: order.concat(res.resultContent)
          })
        } else {
          that.setData({
            hasMoreData: true,
            pageNumber: pageNumber + 1,
            order: order.concat(res.resultContent)
          })
        }
      } else {
        wx.showToast({
          title: res.resultMessage,
          icon: 'none',
          duration: 800
        })
      }
      wx.stopPullDownRefresh();
      wx.hideLoading();
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '正在加载...',
    })
    this.bill('balance');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    wx.showLoading({
      title: '正在刷新...',
    })
    that.setData({
      pageNumber: 1,
      order: []
    })
    if (this.data.detailCheck == 0) {
      this.bill('balance');
    } else {
      this.bill('wechat');
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.hasMoreData) {
      wx.showLoading({
        title: '正在加载...',
      })
      if (this.data.detailCheck == 0) {
        this.bill('balance');
      } else {
        this.bill('wechat');
      }
    } else {
      wx.showToast({
        title: '没有更多数据!',
        icon: 'none',
        duration: 800
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  _switchTab(evt) {
    var {
      currentTab
    } = evt.currentTarget.dataset;
    this.setData({
      currentTab
    })
  }
})